import gradio as gr
import random
import datetime
from fastapi import FastAPI


def get_plot(x):
    random.seed(x)
    xstr = str(random.randint(0, 100))
    xstr += ' ' + str(x)
    xstr += ' ' + str(datetime.datetime.now())
    return xstr


with gr.Blocks(analytics_enabled=False) as demo:
    with gr.Row():
        with gr.Column():
            gr.Markdown("Change the value of the slider to automatically update the plot")
            period = gr.Slider(label="Period of plot", value=1, minimum=0, maximum=10, step=1)
            text = gr.Textbox(interactive=False)

    dep = demo.load(get_plot, inputs=period, outputs=text, every=1)
    # dep = demo.load(get_plot, period, text, every=1)
    # dep = demo.load(get_plot, period, text)
    period.change(get_plot, inputs=period, outputs=text, every=1, cancels=[dep], queue=True, preprocess=False)
    # period.change(get_plot, period, text, every=1)
    # period.change(get_plot, period, text)

# demo.queue().launch(server_name='0.0.0.0', server_port=7776, share=True)
demo.queue()
# demo.launch(server_name='0.0.0.0', server_port=7771, share=True)

CUSTOM_PATH = "/p1/"

app = FastAPI()


@app.get("/")
def read_main():
    return {"message": "This is your main app"}


app = gr.mount_gradio_app(app, demo, path=CUSTOM_PATH)

# uvicorn x00660_continuous_event_my_simple_fastapi:app --host 0.0.0.0 --port 7776 --reload
